package controller.view.impl;

/*
 * simple.java
 * This is a simple, introductory OpenGL program.
 */

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jgl.GL;
import mode.enumeration.Direction;
import model.command.RunnerCommand;
import model.listener.CheckPointListener;
import model.listener.TrafficLight;
import model.vo.CheckPoint;
import model.vo.Position;
import model.vo.Runner;
import utils.NoLimeUtils;
import controller.NLSimulator;
import controller.view.LimeGLAbtraction;
import estimation.impl.MediaMovelJ3;
import estimation.impl.MinimosQuadradosJ3;

public class NLSimulatorGLView extends LimeGLAbtraction implements KeyListener {

	List<NLSimulator> tps = new ArrayList<NLSimulator>();

	public NLSimulatorGLView(List<NLSimulator> transportPlan) {

		tps = transportPlan;

		initDefaultListeners();

		init();

	}

	private void initDefaultListeners() {
		for (NLSimulator m : tps) {

			checksPoints.addAll(m.getCheckpoints());

			m.start();

		}
	}

	public NLSimulatorGLView(NLSimulator transportPlan) {
		tps.add(transportPlan);
		
		initDefaultListeners();

		init();
	}

	protected void drawTransportPlan(NLSimulator motion) {

		myGL.glBegin(GL.GL_QUAD_STRIP);
		myGL.glVertex3f((float) motion.getRunner().getPosition().getX(),
				(float) motion.getRunner().getPosition().getY(), 0.0f);
		myGL.glVertex3f((float) motion.getRunner().getPosition().getX(),
				(float) motion.getRunner().getPosition().getY() + 20, 0.0f);
		myGL.glVertex3f((float) motion.getRunner().getPosition().getX() + 20,
				(float) motion.getRunner().getPosition().getY(), 0.0f);
		myGL.glVertex3f((float) motion.getRunner().getPosition().getX() + 20,
				(float) motion.getRunner().getPosition().getY() + 20, 0.0f);
		myGL.glEnd();

	}

	public void display() {
		/* clear all pixels */
		myGL.glClear(GL.GL_COLOR_BUFFER_BIT);
		myGL.glClearColor(0.5f, 1, 0.5f, 0.7f);
		createScene();

		myGL.glColor3f(1.0f, 0.0f, 0.0f);

		for (NLSimulator m : tps) {

			drawTransportPlan(m);

		}

		drawCheckPoints();

		/*
		 * don't wait! start processing buffered OpenGL routines
		 */
		myGL.glFlush();
		// myUT.glutSwapBuffers(this.getGraphics(), this);
	}

	public void callme(int val) {
		myUT.glutTimerFunc(50, "callme", 10);
		display();
		myUT.glutPostRedisplay();
	}

	private void myinit() {
		myGL.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		myGL.glMatrixMode(GL.GL_PROJECTION);// GL.GL_MODELVIEW
		myGL.glLoadIdentity();
		// myGL.glOrtho(-800f, 800f, -800f, 800f, 0.0f, 1.0f);
		myGL.glOrtho(0f, 1600f, 0f, 1600f, 0.0f, 1.0f);
		// myGL.glViewport(0, 0, 0, 0);
	}

	public void redimension(int h, int w) {
		int largura, altura;

		if (h == 0)
			h = 1;

		largura = w;
		altura = h;

		myGL.glViewport(0, 0, largura, altura);

		myGL.glMatrixMode(GL.GL_PROJECTION);
		myGL.glLoadIdentity();
		display();

		// if (w <= h)
		// myGL.gluOrtho2D (-40.0f, 40.0f, -40.0f*altura/largura,
		// 40.0f*altura/largura);
		myGL.glOrtho(0f, 1600f, 0f, 1600f, 0.0f, 1.0f);

		// else
		// myGL.glOrtho(0f, 1600f, 0f, 1600f, 0.0f, 1.0f);
		// //myGL.gluOrtho2D (-40.0f*largura/altura, 40.0f*largura/altura,
		// -40.0f, 40.0f);

	}

	/*
	 * Declare initial window size, position, and display mode (single buffer
	 * and RGBA). Open window with "hello" in its title bar. Call initialization
	 * routines. Register callback function to display graphics. Enter main loop
	 * and process events.
	 */
	public void init() {
		myUT.glutInitWindowSize(500, 500);
		myUT.glutInitWindowPosition(0, 0);
		myUT.glutCreateWindow(this);

		myinit();

		myUT.glutTimerFunc(250, "callme", 10);
		myUT.glutDisplayFunc("display");
		// myUT.glutReshapeFunc("redimension");

		super.addKeyListener(this);

		myUT.glutMainLoop();

	}

	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if (e.getKeyCode() == 10) {

		}

	}

	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

}
